package com.framework;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 校验组件实现
class ValidationComponent implements Component<Object, Object> {
    private final Map<String, List<ValidationRule>> ruleSets = new HashMap<>();
    
    @Override
    public String getType() {
        return "ValidationComponent";
    }
    
    @Override
    public ComponentOutput<Object> execute(ComponentContext<Object> context) {
        Object request = context.getRequestData();
        List<ValidationResult> errors = new ArrayList<>();
        
        // 添加基于注解的规则
        if (request != null) {
            List<ValidationRule> annotationRules = parseAnnotationRules(request);
            ruleSets.put("annotation", annotationRules);
        }
        
        // 执行所有校验规则
        for (List<ValidationRule> rules : ruleSets.values()) {
            for (ValidationRule rule : rules) {
                ValidationResult result = rule.validate(request);
                if (!result.isValid()) {
                    errors.add(result);
                }
            }
        }
        
        if (!errors.isEmpty()) {
            return ComponentOutput.failure("VALIDATION_FAILED", "参数校验失败", errors);
        }
        
        return ComponentOutput.success(context.getRequestData());
    }
    
    // 解析注解规则
    private List<ValidationRule> parseAnnotationRules(Object object) {
        List<ValidationRule> rules = new ArrayList<>();
        
        for (Field field : object.getClass().getDeclaredFields()) {
            ValidateRule annotation = field.getAnnotation(ValidateRule.class);
            if (annotation != null) {
                String fieldName = field.getName();
                String type = annotation.type();
                
                switch (type) {
                    case "REQUIRED":
                        rules.add(new RequiredRule(fieldName));
                        break;
                    case "LENGTH":
                        rules.add(new LengthRule(fieldName, annotation.min(), annotation.max()));
                        break;
                    case "REGEX":
                        rules.add(new RegexRule(fieldName, annotation.pattern(), annotation.message()));
                        break;
                    case "EMAIL":
                        rules.add(new RegexRule(fieldName, "^\\S+@\\S+\\.\\S+$", "邮箱格式不正确"));
                        break;
                    case "PASSWORD":
                        String passwordMsg = "必须包含至少1个大写字母、1个小写字母和1个数字";
                        rules.add(new LengthRule(fieldName, 8, 30));
                        rules.add(new RegexRule(fieldName, "^(?=.*[a-z])(?=.*[A-Z])(?=.*\\d).+$", passwordMsg));
                        break;
                    case "DATE":
                        rules.add(new DateFormatRule(fieldName, "yyyy-MM-dd", "yyyy/MM/dd"));
                        break;
                }
            }
        }
        return rules;
    }
    
    // 添加规则集
    public void addRuleSet(String key, List<ValidationRule> rules) {
        ruleSets.put(key, rules);
    }
}